using Microsoft.AspNetCore.Mvc;
using WMS.RealTime.IServers.Interfaces;
using WMS.RealTime.Domain.DTOs;

namespace WMS.RealTime.API.Controllers
{
    /// <summary>
    /// 大屏数据控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class DashboardController : ControllerBase
    {
        private readonly IDashboardDataService _dashboardDataService;
        private readonly IRealTimePushService _realTimePushService;
        private readonly ILogger<DashboardController> _logger;

        public DashboardController(
            IDashboardDataService dashboardDataService,
            IRealTimePushService realTimePushService,
            ILogger<DashboardController> logger)
        {
            _dashboardDataService = dashboardDataService;
            _realTimePushService = realTimePushService;
            _logger = logger;
        }

        /// <summary>
        /// 获取今日概况数据
        /// </summary>
        /// <returns>今日概况数据</returns>
        [HttpGet("today-overview")]
        public async Task<ActionResult<DailyOverview>> GetTodayOverview()
        {
            try
            {
                var data = await _dashboardDataService.GetTodayOverviewAsync();
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取今日概况数据失败");
                return StatusCode(500, new { error = "获取今日概况数据失败" });
            }
        }

        /// <summary>
        /// 获取昨日概况数据
        /// </summary>
        /// <returns>昨日概况数据</returns>
        [HttpGet("yesterday-overview")]
        public async Task<ActionResult<DailyOverview>> GetYesterdayOverview()
        {
            try
            {
                var data = await _dashboardDataService.GetYesterdayOverviewAsync();
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取昨日概况数据失败");
                return StatusCode(500, new { error = "获取昨日概况数据失败" });
            }
        }

        /// <summary>
        /// 获取库存占比数据
        /// </summary>
        /// <returns>库存占比数据</returns>
        [HttpGet("inventory-proportion")]
        public async Task<ActionResult<InventoryProportion>> GetInventoryProportion()
        {
            try
            {
                var data = await _dashboardDataService.GetInventoryProportionAsync();
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取库存占比数据失败");
                return StatusCode(500, new { error = "获取库存占比数据失败" });
            }
        }

        /// <summary>
        /// 获取入库统计数据
        /// </summary>
        /// <param name="timeRange">时间范围（天数）</param>
        /// <returns>入库统计数据</returns>
        [HttpGet("inbound-statistics")]
        public async Task<ActionResult<InboundStatistics>> GetInboundStatistics([FromQuery] string timeRange = "7")
        {
            try
            {
                var data = await _dashboardDataService.GetInboundStatisticsAsync(timeRange);
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取入库统计数据失败");
                return StatusCode(500, new { error = "获取入库统计数据失败" });
            }
        }

        /// <summary>
        /// 获取待办事项数据
        /// </summary>
        /// <returns>待办事项数据</returns>
        [HttpGet("todo-items")]
        public async Task<ActionResult<TodoItems>> GetTodoItems()
        {
            try
            {
                var data = await _dashboardDataService.GetTodoItemsAsync();
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取待办事项数据失败");
                return StatusCode(500, new { error = "获取待办事项数据失败" });
            }
        }

        /// <summary>
        /// 获取告警数据
        /// </summary>
        /// <returns>告警数据</returns>
        [HttpGet("alert-data")]
        public async Task<ActionResult<AlertData>> GetAlertData()
        {
            try
            {
                var data = await _dashboardDataService.GetAlertDataAsync();
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取告警数据失败");
                return StatusCode(500, new { error = "获取告警数据失败" });
            }
        }

        /// <summary>
        /// 获取排名数据
        /// </summary>
        /// <returns>排名数据</returns>
        [HttpGet("ranking-data")]
        public async Task<ActionResult<RankingData>> GetRankingData()
        {
            try
            {
                var data = await _dashboardDataService.GetRankingDataAsync();
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取排名数据失败");
                return StatusCode(500, new { error = "获取排名数据失败" });
            }
        }

        /// <summary>
        /// 获取系统消息
        /// </summary>
        /// <param name="count">消息数量</param>
        /// <returns>系统消息列表</returns>
        [HttpGet("system-messages")]
        public async Task<ActionResult<List<string>>> GetSystemMessages([FromQuery] int count = 10)
        {
            try
            {
                var data = await _dashboardDataService.GetSystemMessagesAsync(count);
                return Ok(data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取系统消息失败");
                return StatusCode(500, new { error = "获取系统消息失败" });
            }
        }

        /// <summary>
        /// 获取所有大屏数据
        /// </summary>
        /// <returns>所有大屏数据</returns>
        [HttpGet("all-data")]
        public async Task<ActionResult<object>> GetAllData()
        {
            try
            {
                var result = new
                {
                    TodayOverview = await _dashboardDataService.GetTodayOverviewAsync(),
                    YesterdayOverview = await _dashboardDataService.GetYesterdayOverviewAsync(),
                    InventoryProportion = await _dashboardDataService.GetInventoryProportionAsync(),
                    InboundStatistics = await _dashboardDataService.GetInboundStatisticsAsync(),
                    TodoItems = await _dashboardDataService.GetTodoItemsAsync(),
                    AlertData = await _dashboardDataService.GetAlertDataAsync(),
                    RankingData = await _dashboardDataService.GetRankingDataAsync(),
                    SystemMessages = await _dashboardDataService.GetSystemMessagesAsync()
                };

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有大屏数据失败");
                return StatusCode(500, new { error = "获取所有大屏数据失败" });
            }
        }
    }
} 